<template>
    <div class="page">
      <div v-if="ishouseInformation==0">
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="fullName">
                <el-input size="small" v-model="searchForm.fullName" placeholder="用户姓名" clearable></el-input>
         </el-form-item>
         <el-form-item prop="departmentId">
                  <el-select v-model="searchForm.departmentId" placeholder="请选择隶属部门" size="small" style="width: 100%;" @change="changeDepartment">
                    <el-option
                      v-for="item in departmentList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
         </el-form-item>
         <!-- <el-form-item prop="departmentName">
                  <el-select v-model="searchForm.departmentName" placeholder="请选择部门名称" size="small" style="width: 100%;">
                    <el-option
                      v-for="item in $dictUtils.getDictList('')"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
         </el-form-item> -->
         <el-form-item prop="groupId">
                  <el-select v-model="searchForm.groupId" placeholder="请选择所属小组" size="small" style="width: 100%;">
                    <el-option
                      v-for="item in groupList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
         </el-form-item>
         <!-- <el-form-item prop="groupName">
                  <el-select v-model="searchForm.groupName" placeholder="请选择小组名称" size="small" style="width: 100%;">
                    <el-option
                      v-for="item in $dictUtils.getDictList('')"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
         </el-form-item> -->
         <el-form-item prop="punchEndDate">
                <el-input size="small" v-model="searchForm.punchEndDate" placeholder="打卡地址" clearable></el-input>
         </el-form-item>
         <el-form-item prop="auditStatus">
                  <el-select v-model="searchForm.auditStatus" placeholder="请选择审核状态" size="small" style="width: 100%;">
                    <el-option label="未处理" value="0">未处理</el-option>
                    <el-option label="通过" value="1">通过</el-option> 
                    <el-option label="拒绝" value="2">拒绝</el-option> 
                  </el-select>
         </el-form-item>
         <el-form-item prop="punchType">
                  <el-select v-model="searchForm.punchType" placeholder="请选择打卡类型" size="small" style="width: 100%;">
                    <el-option label="在途打卡" value="1">在途打卡</el-option>
                    <el-option label="楼宇打卡" value="2">楼宇打卡</el-option> 
                  </el-select>
         </el-form-item>
         
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
            <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form size="small" :inline="true" v-show="isImportCollapse"  ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/tcardrecordinfo/tCardcecordInfo/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog>
      <div class="bg-white top">
      <el-row>
        <!-- <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button> -->
        <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button> 
        <el-button-group class="pull-right">
            <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse"></el-button>
            <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
       size="small"
       height="calc(100% - 80px)"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <!-- <el-table-column
        prop="buildingId"
        show-overflow-tooltip
        sortable="custom"
        label="楼宇ID">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:edit')" @click="edit(scope.row.id)">{{scope.row.buildingId}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('tcardrecordinfo:tCardcecordInfo:view')"  @click="view(scope.row.id)">{{scope.row.buildingId}}</el-link>
              <span v-else>{{scope.row.buildingId}}</span>
            </template>
      </el-table-column>
    <el-table-column
        prop="punchNumber"
        show-overflow-tooltip
        sortable="custom"
        label="打卡批次编号">
      </el-table-column>
    <el-table-column
        prop="userId"
        show-overflow-tooltip
        sortable="custom"
        label="打卡人ID">
      </el-table-column>
    <el-table-column
        prop="nikeName"
        show-overflow-tooltip
        sortable="custom"
        label="昵称">
      </el-table-column> -->
    <el-table-column
        prop="fullName"
        show-overflow-tooltip
        sortable="custom"
        label="用户姓名">
      </el-table-column>
    <!-- <el-table-column
        prop="departmentId"
        show-overflow-tooltip
        sortable="custom"
        label="隶属部门ID">
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("", scope.row.departmentId, '-') }}
        </template>
      </el-table-column> -->
    <el-table-column
        prop="departmentName"
        show-overflow-tooltip
        sortable="custom"
        label="部门名称">
        <!-- <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("", scope.row.departmentName, '-') }}
        </template> -->
      </el-table-column>
    <!-- <el-table-column
        prop="groupId"
        show-overflow-tooltip
        sortable="custom"
        label="小组ID">
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("", scope.row.groupId, '-') }}
        </template>
      </el-table-column> -->
    <el-table-column
        prop="groupName"
        show-overflow-tooltip
        sortable="custom"
        label="小组名称">
        <!-- <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("", scope.row.groupName, '-') }}
        </template> -->
      </el-table-column>
    <el-table-column
        prop="punchDate"
        show-overflow-tooltip
        sortable="custom"
        label="打卡时间">
      </el-table-column>
      <el-table-column
        prop="punchPicture"
        show-overflow-tooltip
        sortable="custom"
        label="打卡图片">
      <template slot-scope="scope" v-if="scope.row.punchPicture">
          <!-- <el-image
            style="height: 50px;width:50px;margin-right:10px;"
            :src="src" v-for="(src, index) in scope.row.punchPicture.split('|')" :key="index"
            :preview-src-list="scope.row.punchPicture.split('|')">
          </el-image> -->
          <el-image
            style="height: 50px;width:50px;margin-right:10px;"
            :src="scope.row.punchPicture"
            :preview-src-list="scope.row.punchPicture.split('|')">
          </el-image>
        </template>
      </el-table-column>
    <el-table-column
        prop="punchAddress"
        show-overflow-tooltip
        sortable="custom"
        label="打卡地址">
      </el-table-column>
    <el-table-column
        prop="auditStatus"
        show-overflow-tooltip
        sortable="custom"
        label="审核状态">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.auditStatus == 0">未处理</el-tag>
          <el-tag type="success" v-if="scope.row.auditStatus == 1">已通过</el-tag>
          <el-tag type="danger" v-if="scope.row.auditStatus == 2">已拒绝</el-tag>
        </template>
      </el-table-column>
    <el-table-column
        prop="punchType"
        show-overflow-tooltip
        sortable="custom"
        label="打卡类型">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.punchType == 1">在途打卡</el-tag>
          <el-tag type="success" v-if="scope.row.punchType == 2">楼宇打卡</el-tag>
        </template>
      </el-table-column>
    <!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息">
      </el-table-column> -->
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <!-- <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button> -->
          <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button> 
          <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:view') && scope.row.auditStatus == 0" type="text" icon="el-icon-edit" size="small" @click="audit(scope.row.id,scope.row.punchType,1)">通过</el-button>
          <el-button v-if="hasPermission('tcardrecordinfo:tCardcecordInfo:view') && scope.row.auditStatus == 0" type="text" icon="el-icon-edit" size="small" @click="jj(scope.row.id,scope.row.punchType,2)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
        <!-- 弹窗, 新增 / 修改 -->
    <TCardcecordInfoForm  ref="tCardcecordInfoForm" @refreshDataList="refreshList"></TCardcecordInfoForm>
  </div>
  <div v-else-if="ishouseInformation==1">
        <div class="query-form">
            <InputBox @setAccess="setAccess">
              <template v-slot:up>
                <div class="header-top">
                  <span>请输入访问密码</span>
                  <input type="password" v-model="accessPassword" placeholder="单行输入">
                </div>
              </template>
          </InputBox>
        </div>
    </div>
    <div v-else-if="ishouseInformation==2">
        <div class="query-form">
            <InputBox @setAccess="setAccess" :width="448">
              <template v-slot:up>
                <div class="header-top">
                  <span>请输入原访问密码</span>
                  <input type="text" v-model="initialPassword" placeholder="单行输入">
                </div>
                <div class="header-top">
                  <span>请输入修改后的访问密码</span>
                  <input type="text" v-model="changePassword" placeholder="单行输入">
                </div>
                <div class="header-top">
                  <span>请确认访问密码</span>
                  <input type="text" v-model="ConfirmPassword" placeholder="单行输入">
                </div>
              </template>
          </InputBox>
        </div>
    </div>
  </div>
</template>

<script>
  import TCardcecordInfoForm from './TCardcecordInfoForm'
  import InputBox from '@/components/Inputbox/Inputbox.vue'
  export default {
    data () {
      return {
        searchForm: {
          nikeName: '',
          fullName: '',
          departmentId: '',
          departmentName: '',
          groupId: '',
          groupName: '',
          punchEndDate: '',
          auditStatus: '',
          punchType: '',
          remarks: ''
        },
        dataList: [],
        initialPassword:"",//原访问密码
        changePassword:"",//修改后的访问密码
        ConfirmPassword:"",//确认访问密码
        ishouseInformation:1,
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isImportCollapse: false,
        loading: false,
        departmentList: [],
        groupList: []
      }
    },
    components: {
      TCardcecordInfoForm,
      InputBox
    },
    activated () {
      this.getDepartmentInfo(),
      this.refreshList()
      if(this.ishouseInformation!=1){
		    this.ishouseInformation =1
		    this.accessPassword =""
    	}
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/tcardrecordinfo/tCardcecordInfo/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            console.log(data)
            data.page.list.forEach(element => {
              element.punchPicture = 'https://dev.zhumanman.com.cn/pigfull/upload/' + element.punchPicture
            });
          
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 获取所有部门信息
      getDepartmentInfo() {
        this.$http({
          url: '/tdepartmentinfo/tDepartmentInfo/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': 999999,
            'orderBy': this.orderBy
          }
        }).then(({data}) => {
          this.departmentList = []
         data.page.list.forEach(e => {
           this.departmentList.push({
            value:e.id,
            label:e.departmentName
          })
         });
        })
      },
      // 选中团队获取想对应的小组信息
      changeDepartment(e) {
        this.$http({
          url: '/tgroupinfo/tGroupInfo/queryGroupInfoByDepId',
          method: 'get',
          params: {
            id:e
          }
        }).then(({data}) => {
          this.groupList = []
         data.result.forEach(e => {
           this.groupList.push({
            value:e.id,
            label:e.groupName,
          })
         });
        })
      },
      //访问密码确认取消按钮
      setAccess(i){
        if(i==0){
          this.accessPassword="";
          this.initialPassword="";
          this.changePassword="";
          this.ConfirmPassword="";
          return
        }else{
          if(this.ishouseInformation==1){
            if(this.accessPassword==""){
              this.$message.error('访问密码不能为空');
              return
            }
            this.$http({
              url: `/tpasswordinfo/tPasswordInfo/cmsLogin?type=5&password=${this.accessPassword}`,
              method: 'get'
            }).then(({data}) => {
              if(data.code==200&&data.msg=="操作成功"){
                this.$message.success(data.msg)
                this.ishouseInformation = 0
              }else{
                this.$message.error(data.msg);
              }
            })
          }else if(this.ishouseInformation==2){
            if(this.initialPassword=="" || this.changePassword=="" || this.ConfirmPassword==""){
              this.$message.error('输入框不能为空');
              return
            }

            if(this.changePassword != this.ConfirmPassword){
              this.$message.error('确认密码不相同');
              return
            }

            this.$http({
              url: `/tpasswordinfo/tPasswordInfo/updatePasswrod`,
              method: 'post',
              data:{
                oldPassword:this.initialPassword,
                newPassword:this.changePassword,
                type:5
              }
            }).then(({data}) => {
              if(data.code==200&&data.msg=="修改成功！"){
                this.$message.success(data.msg)
                this.ishouseInformation = 1
              }else{
                this.$message.error(data.msg);
              }
            })
          }
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 通过
      audit(id,type,status){
        console.log(123)
        this.loading = true
          this.$http({
            url: '/tcardrecordinfo/tCardcecordInfo/audit',
            method: 'post',
            params: {
              'id': id,
              'punchType':type,
              "auditStatus":status
            }
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
      },
      // 拒绝
      jj(id,type,status){
        this.$http({
        url: '/tcardrecordinfo/tCardcecordInfo/audit',
        method: 'post',
        params: {
          'id': id,
          'punchType':type,
          "auditStatus":status
        }
      }).then(({data}) => {
        if (data && data.success) {
          this.$message.success(data.msg)
          this.refreshList()
        }
        this.loading = false
      })
      },
      // 新增
      add () {
        this.$refs.tCardcecordInfoForm.init('add', '')
      },
      // 修改
      edit (id,type) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.tCardcecordInfoForm.init('edit', id)
      },
      // 查看
      view (id,type) {
        this.$refs.tCardcecordInfoForm.init('view', id)
      },
      // 删除
      del (id,type) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/tcardrecordinfo/tCardcecordInfo/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/tcardrecordinfo/tCardcecordInfo/import/template')
      },
      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/tcardrecordinfo/tCardcecordInfo/export', params)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>